package com.cskaoyan.bytestream.buffer;


import java.io.*;

/*
        不使用缓冲流:
         一次复制一个字节
         一次复制一个字节数组

         使用缓冲流
          一次复制一个字节
          一次复制一个字节数组

        copyByByte: 91649
        copyByByteInBuf: 866

        copyByBytes: 124
        copyByBytesInBuf: 41


 */
public class Compare {
    public static void main(String[] args) throws IOException {

        String src = "d:\\demo\\vedio.mp4";

        String dest1 = "copy1.mp4";
        String dest2 = "copy2.mp4";
        String dest3 = "copy3.mp4";
        String dest4 = "copy4.mp4";

        copyByByte(src, dest1);
//        copyByByteInBuf(src, dest2);
//        copyByBytes(src, dest3);
//        copyByBytesInBuf(src, dest4);



    }

    // 不使用缓冲流，一次复制一个字节
    public static void copyByByte(String src, String dest) throws IOException {
        // 1.创建流对象
      InputStream in = new FileInputStream(src);
      OutputStream out = new FileOutputStream(dest);

       //文件复制
        int readByte;
        // 该方法先获取文件复制之前的时间点(以毫秒为单位)
        long start = System.currentTimeMillis();
        while ((readByte = in.read()) != -1) {
            out.write(readByte);
        }
        long end = System.currentTimeMillis();
        System.out.println("copyByByte: " + (end - start));
        in.close();
        out.close();
    }


    //使用缓冲流，一次复制一个字节
    public static void copyByByteInBuf(String src, String dest) throws IOException {

        // 1.创建流对象
        InputStream in = new BufferedInputStream(new FileInputStream(src));
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));

        //文件复制
        int readByte;
        long start = System.currentTimeMillis();
        while ((readByte = in.read()) != -1) {
            out.write(readByte);
        }
        long end = System.currentTimeMillis();
        System.out.println("copyByByteInBuf: " + (end - start));
        in.close();
        out.close();



    }

    // 不使用缓冲区，一次复制一个字节数组
    public static void copyByBytes(String src, String dest) throws IOException {
        // 1.创建流对象
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        //文件复制
        int len;
        byte[] byteBuf = new byte[1024];
        long start = System.currentTimeMillis();
        while ((len = in.read(byteBuf)) != -1) {
            out.write(byteBuf,0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println("copyByBytes: " + (end - start));
        in.close();
        out.close();

    }

    // 使用缓冲流一次复制一个字节数组
    public static void copyByBytesInBuf(String src, String dest) throws IOException {


        // 1.创建流对象
        InputStream in = new BufferedInputStream(new FileInputStream(src));
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));

        //文件复制
        int len;
        byte[] byteBuf = new byte[1024];
        long start = System.currentTimeMillis();
        while ((len = in.read(byteBuf)) != -1) {
            out.write(byteBuf,0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println("copyByBytesInBuf: " + (end - start));
        in.close();
        out.close();

    }

}
